package com.lzb.yabbs.app;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.ProgressBar;

import com.gitonway.lee.niftynotification.lib.Effects;
import com.gitonway.lee.niftynotification.lib.NiftyNotificationView;
import com.lzb.yabbs.AppConfig;
import com.lzb.yabbs.R;
import com.lzb.yabbs.adapter.CardListAdapter;
import com.lzb.yabbs.dao.Board;
import com.lzb.yabbs.dao.Card;
import com.lzb.yabbs.interfaces.GetCardsCallback;
import com.lzb.yabbs.net.GetCardsFromNet;
import com.melnykov.fab.FloatingActionButton;

import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zaibinng on 2015/9/9.
 */
public class CardListActivity extends AppCompatActivity implements AdapterView.OnItemClickListener, View.OnClickListener {

    private int boardId;
    private List<Card> mCards=new ArrayList<>();

    private final int GET_CARD_SUCESS=0x123;
    private final int GET_CARD_FAIL=0x1234;

    private ListView mCardLv;
    private CardListAdapter cardListAdapter;
    private ProgressBar mProgressBar;
    private FloatingActionButton fab;

    public Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);

            switch (msg.what){
                case GET_CARD_SUCESS:
                    if(mCards.size()<=0){

                        NiftyNotificationView.build(CardListActivity.this, "当前版块无数据", Effects.flip, R.id.mll)
                                .setIcon(R.mipmap.ic_launcher).show();
                    }
                    fab.setVisibility(View.VISIBLE);
                    cardListAdapter.addCards(mCards);
                    mProgressBar.setVisibility(View.GONE);
                    break;
                case GET_CARD_FAIL:
                    NiftyNotificationView.build(CardListActivity.this, "加载失败，检查网络连接", Effects.flip, R.id.mll)
                            .setIcon(R.mipmap.ic_launcher).    //You must call this method if you use ThumbSlider effect
                            show();
                    mProgressBar.setVisibility(View.GONE);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.card_list_layout);
        Board board= (Board) getIntent().getSerializableExtra("board");
        boardId=board.getId();
        setTitle(board.getName());

        mCardLv= (ListView) findViewById(R.id.card_lv);
        mProgressBar= (ProgressBar) findViewById(R.id.progressbar);
        cardListAdapter=new CardListAdapter(mCards,this);
        mCardLv.setAdapter(cardListAdapter);
        mCardLv.setOnItemClickListener(this);

        fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.attachToListView(mCardLv);
        fab.setOnClickListener(this);

        getCards(0);
    }


    @Override
    public void setTitle(CharSequence title) {
        super.setTitle(title);
    }

    private void getCards(int page){
        List<BasicNameValuePair> pairs=new ArrayList<>();
        BasicNameValuePair pair=new BasicNameValuePair("page",page+"");
        BasicNameValuePair pair1=new BasicNameValuePair("boardId",boardId+"");
        pairs.add(pair);
        pairs.add(pair1);

        GetCardsFromNet.getCards(AppConfig.URL_GET_CARD_FROM_BOARD, new GetCardsCallback() {
            @Override
            public void getCardsSuccess(List<Card> cards) {
                if(cards!=null)
                    mCards=cards;
                mHandler.sendEmptyMessage(GET_CARD_SUCESS);
            }

            @Override
            public void getCardFail(int reason) {
                mHandler.sendEmptyMessage(GET_CARD_FAIL);
            }
        },pairs);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent=new Intent(this,CardInfoActivity.class);
        intent.putExtra("card",mCards.get(position));
        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        if(R.id.fab==v.getId()){
            Intent intent=new Intent(this,SendCardActivity.class);
            intent.putExtra("boardId",boardId);
            intent.putExtra("boardName",getTitle());
            startActivity(intent);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode==AppConfig.SEND_CARD_SUCESS_BACK_CODE){
            getCards(0);
        }
    }
}
